using System;
using System.IO;
using System.Net;
using System.Text;
using NUnit.Framework;

namespace Crawler.Tests
{
	/// <summary>
	/// Summary description for TestParser.
	/// </summary>
	[TestFixture]
	public class TestParser
	{
		ParserPool parserPool;
		Uri site;
		string path;
		ICentralFactory factory;
		UrlStack urlStack;

		public string GetHTMLSource(Uri site) 
		{
			WebRequest wReq = WebRequest.Create(site);
			WebResponse wResp = wReq.GetResponse();
			StreamReader reader = new StreamReader(wResp.GetResponseStream() , Encoding.ASCII);
			String stringDump = reader.ReadToEnd();
			wResp.Close() ;
			return stringDump;
		}

		[SetUp]
		public void Initialize()
		{
			factory = CentralFactory.centralFactory;
			factory.Add(DownloadStore.NULL_DOWNLOADSTORE);
			parserPool = new ParserPool(1) ;
			urlStack = new UrlStack(null);
			factory.Add(new ResourcePath(@"e:/testdata"));
			factory.Add(urlStack ) ;
			factory.Add(@"http://127.0.0.1/");
			urlStack.UrlAddedToUrlStack += new UrlAddedToUrlStackHandler(Fetcher.NULL_FETCHER.NotifyFetcherAboutUrlAdditionToUrlStack);
		}
		
		[Test]
		public void TestNewParser()
		{
			path = @"http://127.0.0.1/manual";
			site = new Uri(path);
			Assert.IsTrue( urlStack.Count == 0) ;
			parserPool.ParsePage(new Dumpable(site.AbsoluteUri, GetHTMLSource(site)), new Key(path));
			Assert.IsTrue(urlStack.Count>0);
		}
		
	}
}
